/*
 * Copyright (c) 2012 Keerthi Ramachandran .
 */

package com.kee.example.runner;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.kee.example.domain.Contact;
import com.kee.example.domain.CustImage;
import com.kee.example.domain.Event;
import com.kee.example.domain.Order;
import com.kee.example.domain.OrderKey;
import com.kee.example.domain.OrderTypeEnm;
import com.kee.example.domain.Person;
import com.kee.example.domain.Phone;
import com.kee.example.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.metadata.ClassMetadata;

/**
 * Created with IntelliJ IDEA.
 * User: keerthi
 * Date: 12/8/12  6:17 PM
 */
public class EventRunner {
    public static final String EVENT_STRING = "Event String";
    public static final Date EVENT_DATE = new Date();
    public static final String FIRST_NAME = "FirstName";
    public static final String LAST_NAME = "LastName";
    private static final String PERSON_EMAIL = "email@email.mail";
    private static final String URL1 = "url1";
    private static final String URL2 = "url2";

    public static void main(String[] args) {
        EventRunner runner = new EventRunner();
        runner.printAllLoadedEntities();
        runner.popUpInitData();
        runner.printEvents();
        runner.printPerson();
        runner.printOrders();
        HibernateUtil.getSessionFactory().close();
    }

    private void printOrders() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Order> orderList = session.createQuery("from Order").list();
        for(Order order:orderList){
            System.out.println("order.getOrderKey().getOrderId() = " + order.getOrderKey().getOrderId());
            System.out.println("order.getOrderKey().getOrderType().toString() = " + order.getOrderKey().getOrderType().toString());
            System.out.println("order.getOrderKey().getOrderType() = " + order.getOrderKey().getOrderType());
            System.out.println("order.getOrderNumber() = " + order.getOrderNumber());
            for(String imgUrl: order.getOrderImgUrl()){
                System.out.println("imgUrl = " + imgUrl);
            }
            for(Object imgUrl: order.getOrderImgUrls()){
                System.out.println("imgUrl.toString() = " + imgUrl.toString());
            }
            for(String imgUrl: order.getImagesSet()){
                System.out.println("imgUrl = " + imgUrl);
            }
            Map<Long,String> longStringMap = order.getImagesMap();
            for(Long aLong: longStringMap.keySet()){
                System.out.println("longStringMap.get("+aLong+") = " + longStringMap.get(aLong));
            }
            for(CustImage image:order.getCustImagesSet()){
                System.out.println("image = " + image);
            }

        }
    }

    private void popUpInitData() {
        createAndStoreEvent();
        createAndStoreOrder();
        createAndStorePersonWithEvent();
    }

    /**
     * Using Class with Composite Id
     */
    private void createAndStoreOrder() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Order order = new Order();
        order.setOrderKey(new OrderKey(1234456789l,OrderTypeEnm.PO));
        order.setOrderNumber(1234456789L);
        List<String> urlList = new ArrayList<String>(2);
        urlList.add(URL1);
        urlList.add(URL2);
        order.setOrderImgUrl(urlList);
        order.setOrderImgUrls(urlList);
        Set<String> urlSet = new HashSet<String>(2);
        urlSet.add(URL1);
        urlSet.add(URL2);
        order.setImagesSet(urlSet);
        Map<Long,String> longStringMap = new HashMap<Long, String>(2);
        longStringMap.put(1L,URL1);
        longStringMap.put(2L,URL2);
        order.setImagesMap(longStringMap);

        CustImage custImage1 = new CustImage(1,"IMG","IMG",1L);
        CustImage custImage2= new CustImage(2,"IMG","IMG",2L);
        Set<CustImage> custImages = new HashSet<CustImage>(2);
        custImages.add(custImage1);
        custImages.add(custImage2);
        order.setCustImagesSet(custImages);
        session.save(order);
        session.getTransaction().commit();
    }

    private void createAndStoreEvent() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Event event = new Event();
        event.setEventDate(EVENT_DATE);
        event.setEventString(EVENT_STRING);
        session.save(event);
        session.getTransaction().commit();
    }

    private List<Event> eventList() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Event> eventList = session.createQuery("from Event").list();
        session.getTransaction().commit();
        return eventList;
    }

    private void printEvents() {
        List<Event> eventList = eventList();
        for (Event event : eventList) {
            System.out.println("event.getId() = " + event.getId());
            System.out.println("event.getEventDate().toString() = " + event.getEventDate().toString());
            System.out.println("event.getEventString() = " + event.getEventString());
        }
    }

    private void printEvents(Event event) {
        System.out.println("event.getId() = " + event.getId());
        System.out.println("event.getEventDate().toString() = " + event.getEventDate().toString());
        System.out.println("event.getEventString() = " + event.getEventString());
    }

    private void printPerson() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Person> personList = session.createQuery("from Person").list();
        for (Person person : personList) {
            System.out.println("person.getId() = " + person.getId());
            System.out.println("person.getFirstName() = " + person.getFirstName());
            System.out.println("person.getLastName() = " + person.getLastName());
            System.out.println("person.getHolidayContact().getRecipient() = " + person.getHolidayContact().getRecipient());
            System.out.println("person.getHolidayContact().getPhone() = " + person.getHolidayContact().getPhone());
            System.out.println("person.getWeekDayContact().getRecipient() = " + person.getWeekDayContact().getRecipient());
            System.out.println("person.getWeekDayContact().getPhone() = " + person.getWeekDayContact().getPhone());
            Set<Event> eventSet = person.getEvents();
            for (Event event : eventSet) {
                printEvents(event);
            }
            Set<String> emailStrings = person.getEmailAddress();
            for(String s:emailStrings){
                System.out.println("emailString = " + s);
            }
        }
        session.getTransaction().commit();
    }

    private void createAndStorePersonWithEvent() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Event event = new Event();
        event.setEventDate(EVENT_DATE);
        event.setEventString(EVENT_STRING);
        session.save(event);
        Person person = new Person();
        person.setFirstName(FIRST_NAME);
        person.setLastName(LAST_NAME);
        person.getEmailAddress().add(PERSON_EMAIL);
        Phone phone = new Phone();
        phone.setAreaCode("044");
        phone.setPhoneNo("22230409");
        Order order = new Order();
        order.setOrderKey(new OrderKey(123456789,OrderTypeEnm.PO));
        order.setOrderNumber(123456789);
        session.save(order);
        Contact contact = new Contact();
        contact.setRecipient("Recipient 1");
        contact.setPhone(phone);
        contact.setOrder(order);
        person.setWeekDayContact(contact);
        person.setHolidayContact(contact);

        Set eventSet = new HashSet(1);
        eventSet.add(event);
        person.setEvents(eventSet);
        session.save(person);
        session.getTransaction().commit();
    }

    private void printAllLoadedEntities(){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        try {
            System.err.println("querying all the managed entities...");
            final Map metadataMap = session.getSessionFactory().getAllClassMetadata();
            for (Object key : metadataMap.keySet()) {
                final ClassMetadata classMetadata = (ClassMetadata) metadataMap.get(key);
                final String entityName = classMetadata.getEntityName();
                System.err.println("Loaded Entities are     " +entityName);
            }
        } finally {
            session.getTransaction().commit();
        }
    }
}
